using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            
            try
            {
                serialPort.Open();
                serialPort.Write("Test");
            }
            catch (System.IO.IOException ioe)
            {
                MessageBox.Show(ioe.Message);
            }
            catch (System.InvalidOperationException invalidOpEx)
            {
                MessageBox.Show(invalidOpEx.Message);
            }
            
        }

        private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
           if( e.EventType == System.IO.Ports.SerialData.Chars)
           {
               char[] buffer = new char[serialPort.ReadBufferSize];
               serialPort.Read(buffer, 0, serialPort.ReadBufferSize);

               String msg = new String(buffer);               
               MessageBox.Show(msg);
           }
        }

        private void serialPort_ErrorReceived(object sender, System.IO.Ports.SerialErrorReceivedEventArgs e)
        {

        }

        private void serialPort_PinChanged(object sender, System.IO.Ports.SerialPinChangedEventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            serialPort.DiscardInBuffer();
            serialPort.DiscardOutBuffer();
            serialPort.Close();
        }
    }
}